package com.ambulance.project.biz.controller.manager;

import com.ambulance.common.utils.poi.ExcelUtil;
import com.ambulance.framework.aspectj.lang.annotation.Log;
import com.ambulance.framework.aspectj.lang.enums.BusinessType;
import com.ambulance.framework.web.controller.BaseController;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.page.TableDataInfo;
import com.ambulance.project.biz.dto.admin.AdminStatisticsDto;
import com.ambulance.project.biz.service.CarDeviceService;
import com.ambulance.project.biz.service.MyCustomerService;
import com.ambulance.project.manager.domain.BizCardevice;
import com.ambulance.project.manager.domain.BizDispatch;
import com.ambulance.project.manager.service.IBizDispatchService;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 调度记录基本信息Controller
 *
 * @author zr
 * @date 2020-05-11
 */
@RestController
@RequestMapping("/manager/dispatch")
public class BizDispatchController extends BaseController {
    @Autowired
    private IBizDispatchService bizDispatchService;


    @Autowired
    private MyCustomerService myCustomerService;
    @Autowired
    private CarDeviceService carDeviceService;

    /**
     * 获取调度统计数据
     *
     * @param
     * @return
     */
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数不正确"),
            @ApiResponse(code = 404, message = "404请求路径不正确"),
            @ApiResponse(code = 200, response = AdminStatisticsDto.class, message = "响应")
    })
    @GetMapping("/list/admin/statics")
    public Object listAdminStatisticsDto() {
        Map<String, AdminStatisticsDto> currentMonthAndLastData = myCustomerService.getCurrentMonthAndLastData();
        return AjaxResult.success(currentMonthAndLastData);
    }

    /**
     * 查询调度记录基本信息列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:dispatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizDispatch bizDispatch) {
        startPage();
        List<BizDispatch> list = bizDispatchService.selectBizDispatchList(bizDispatch);
        return getDataTable(list);
    }

    /**
     * 条件查询调度列表
     *
     * @param bizDispatch
     * @return
     */
    @GetMapping("/customer/list")
    public TableDataInfo customerList(BizDispatch bizDispatch) {
        startPage();
        List<BizDispatch> list = myCustomerService.getDispatchList(bizDispatch);
//        List<BizDispatch> list = bizDispatchService.selectBizDispatchList(bizDispatch);
        return getDataTable(list);
    }

    /**
     * 设备编号查询调度
     *
     * @return
     */
    @GetMapping("/customer/devicelist")
    public TableDataInfo deviceList(BizCardevice bizCardevice) {
        final BizDispatch bizDispatch = new BizDispatch();
        bizDispatch.setCarDeviceID(bizCardevice.getDeviceID().toString());
        startPage();
        List<BizDispatch> list = myCustomerService.getDispatchList(bizDispatch);
//        List<BizDispatch> list = bizDispatchService.selectBizDispatchList(bizDispatch);
        return getDataTable(list);
    }

    /**
     * 导出调度记录基本信息列表
     */
    //@PreAuthorize("@ss.hasPermi('manager:dispatch:export')")
    @Log(title = "调度记录基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizDispatch bizDispatch) {
        List<BizDispatch> list = bizDispatchService.selectBizDispatchList(bizDispatch);
        ExcelUtil<BizDispatch> util = new ExcelUtil<BizDispatch>(BizDispatch.class);
        return util.exportExcel(list, "dispatch");
    }

    /**
     * 获取调度记录基本信息详细信息
     */
//    //@PreAuthorize("@ss.hasPermi('manager:dispatch:query')")
    @GetMapping(value = "/{dispatchID}")
    public AjaxResult getInfo(@PathVariable("dispatchID") Long dispatchID) {
        return AjaxResult.success(bizDispatchService.selectBizDispatchById2(dispatchID));
    }

    /**
     * 新增调度记录基本信息
     */
    //@PreAuthorize("@ss.hasPermi('manager:dispatch:add')")
    @Log(title = "调度记录基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizDispatch bizDispatch) {
        return toAjax(bizDispatchService.insertBizDispatch(bizDispatch));
    }

    /**
     * 修改调度记录基本信息
     */
    //@PreAuthorize("@ss.hasPermi('manager:dispatch:edit')")
    @Log(title = "调度记录基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizDispatch bizDispatch) {
        return toAjax(bizDispatchService.updateBizDispatch(bizDispatch));
    }


    @Log(title = "取消调度", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/cancel")
    public AjaxResult cancel(@RequestBody BizDispatch bizDispatch) {
        myCustomerService.cancelCar(bizDispatch.getDispatchID().intValue(), bizDispatch.getCancelReason());
        return AjaxResult.success();
    }


    @Log(title = "调度派车", businessType = BusinessType.UPDATE)
    @PutMapping(value = "doDispatcher")
    public AjaxResult doDispatcher(@RequestBody BizDispatch bizDispatch) {

        try {
            bizDispatchService.doDispatcher(bizDispatch);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error();
        }


    }


    /**
     * 删除调度记录基本信息
     */
    //@PreAuthorize("@ss.hasPermi('manager:dispatch:remove')")
    @Log(title = "调度记录基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dispatchIDs}")
    public AjaxResult remove(@PathVariable Long[] dispatchIDs) {
        return toAjax(bizDispatchService.deleteBizDispatchByIds(dispatchIDs));
    }
}
